<?php
$context = stream_context_create(['http' => [
    'method' => 'POST',
    'header' => 'Content-type: application/x-www-form-urlencoded',
    'content' => http_build_query([
        'xh' => 'admin',
        'pwd' => '12345',
        'rememberMe' => 'on',
    ]),
    'follow_location' => 0,//禁用重定向，目的是获取登录成功之后从响应头返回的sessionID的值
    //'max_redirects' => 1,//跟随重定向的最大此时。值为1或更少则意味不跟随重定向，默认值是20.
]]);
//$html = file_get_contents('http://localhost/login.php',context: $context);
//echo $html;
//print_r($http_response_header);//获取响应头
$headers = get_headers('http://localhost/login.php',true,$context);
//print_r($headers);

$sessionId = null;
foreach ($headers['Set-Cookie'] as $header){
    if (str_starts_with($header,'PHPSESSID=')){
        $sessionId = $header;
        break;
    }
}
echo "sessionId = $sessionId\n";

$context = stream_context_create(['http' => [
    'header' => "Cookie: $sessionId",
]]);
$html = file_get_contents('http://localhost/index.php',context:$context);
preg_match_all('@<tr><td>(\d+)</td><td>([^<]+)</td><td>([^<]+)</td>@',$html,$m,PREG_SET_ORDER);
//print_r($m[0]);
foreach ($m as $user){
    echo "$user[1]\t$user[2]\t$user[3]\n";
    //删除注册的用户 ， xh = $user[1]
    file_get_contents('http://localhost/delete.php?xh='.$user[1], context:$context);
}


